package com.idreamsky.core.lib.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUitl {

    private static final SimpleDateFormat TIMEFORMAT = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    private static final SimpleDateFormat DATETOSTRING = new SimpleDateFormat("yyyy-MM-dd");
    private static final SimpleDateFormat YearMonthDate = new SimpleDateFormat("yyyy年MM月dd日");
    private static final SimpleDateFormat MMDDHHMM = new SimpleDateFormat("MM月dd日 HH:mm");
    private static final SimpleDateFormat YYYYMMDD = new SimpleDateFormat("yyyyMMdd");
    private static final SimpleDateFormat HHmm = new SimpleDateFormat("HH:mm");
    private static final SimpleDateFormat HHmmss = new SimpleDateFormat("HH:mm:ss");
    private static final SimpleDateFormat YYYYMMDDHHMMSSS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    private static SimpleDateFormat dateFormaterWithoutSS = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    private static SimpleDateFormat dateFormaterTime = new SimpleDateFormat("HH:mm");
    private static SimpleDateFormat dateFormaterDateAndTime = new SimpleDateFormat("MM-dd HH:mm");

    /**
     * 当前时间和timeMillis对比后返回不同的格式
     * @author William.cheng
     * @version 创建时间：2011-12-28 下午3:37:50
     * @param timeMillis
     * @return
     */
    public static CharSequence formatTime(long timeMillis) {
        Date date = new Date(timeMillis);
        long currentTime = System.currentTimeMillis();
        Date currentDate = new Date(currentTime);
        long diff = currentTime - timeMillis;
        long diffTime = diff/(60000);
        long diffDate = currentDate.getDate() - date.getDate();
        long diffMonth = currentDate.getMonth() - date.getMonth();
        long diffYear = currentDate.getYear() - date.getYear();
        if (diffYear != 0) {
            return dateFormaterWithoutSS.format(date);
        } else if (diffMonth == 0 && diffDate == 0) {
            if (diffTime <= 5) {
                return "刚刚";
            } else if (diffTime <= 30) {
                return diffTime + " 分钟前";
            } else {
                return "今天 " + dateFormaterTime.format(date);
            }
        } else if (diffMonth == 0 && diffDate == 1) {
            return "昨天 " + dateFormaterTime.format(date);
        } else {
            return dateFormaterDateAndTime.format(date);
        }
    }
    
    public static String getMMDDHHMM(long time) {
        return MMDDHHMM.format(time);
    }
    
    public static String getTimeFileName() {
        return TIMEFORMAT.format(new Date());
    }
    
    public static String getSystemTimeFileName() {
        return String.valueOf(System.nanoTime());
    }
    
    public static String getDate() {
        return YYYYMMDDHHMMSSS.format(new Date());
    }
    
    public static Date getDate(String date) {
        try {
            return DATETOSTRING.parse(date);
        } catch (Exception e) {
            return null;
        }
    }
    
    public static String getDate(Date date) {
        try {
            return DATETOSTRING.format(date);
        } catch (Exception e) {
            return null;
        }
    }
    
    public static String getYmd(Date date) {
        try {
            return YearMonthDate.format(date);
        } catch (Exception e) {
            return null;
        }
    }
    
    public static String getYyyymmdd(Date date) {
        try {
            return YYYYMMDD.format(date);
        } catch (Exception e) {
            return null;
        }
    }
       
    public static String getHHmm(Date date) {
        try {
            return HHmm.format(date);
        } catch (Exception e) {
            return null;
        }
    }
    
    public static String getHHmm(long date) {
        return getHHmm(new Date(date));
    }
    
    public static String getHHmmss(Date date) {
        try {
            return HHmmss.format(date);
        } catch (Exception e) {
            return null;
        }
    }
    /**
     * 取得指定天数后的日期
     */
    public static String nextDate(int day) {
        Calendar cal = Calendar.getInstance();
        Date date = new Date();
        try {
            cal.setTime(date); 
            cal.add(Calendar.DATE, day);
            return getDate(cal.getTime());
        } catch (Exception e) {
            return getDate(date);
        }
    }
    
}